//
//  SettingViewController.m
//  Fengshui
//
//  Created by are on 13-5-22.
//
//

#import "SettingViewController.h"

@interface SettingViewController () <AreaPickerDelegate>

@property (strong, nonatomic) NSString *areaValue, *cityValue;
@property (strong, nonatomic) AreaPickerViewController *locateAndAreaPicker;

@end

@implementation SettingViewController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        // Custom initialization
    }
    return self;
}
-(void)toolbarDoneButton:(id)sender
{
    if (self.locateAndAreaPicker.view.superview) {

        [UIView animateWithDuration:0.3f animations:^(void){
            [self.locateAndAreaPicker.view setFrame:CGRectMake(0, self.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.locateAndAreaPicker.view.frame.size.height)];
            [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height, 320, self.keyToolBar.frame.size.height)];
        }];
        
        [self.locateAndAreaPicker.view  removeFromSuperview];
        [self.keyToolBar removeFromSuperview];
        [self.locateAndAreaPicker release];
        self.locateAndAreaPicker = nil;
    }

    if (self.baziPickerView.superview) {


        [UIView animateWithDuration:0.3f animations:^(void){
            [self.baziPickerView setFrame:CGRectMake(0, self.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.baziPickerView.frame.size.height)];
            [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height, 320, self.keyToolBar.frame.size.height)];
        }];
        [self.baziPickerView  removeFromSuperview];
        [self.keyToolBar removeFromSuperview];
    }
    [self.m_scrollView setFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
    [self.m_scrollView setContentSize:CGSizeMake(320, self.view.frame.size.height)];

}
-(void)viewWillAppear:(BOOL)animated{
    
    keyBoardController=[[UIKeyboardViewController alloc] initWithControllerDelegate:self];
    [keyBoardController addToolbarToKeyboard];


    UIBarButtonItem *spaceBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                  target:nil
                                                                                  action:nil];
    
    UIBarButtonItem *doneBarItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"确定", @"")
                                                                    style:UIBarButtonItemStyleDone
                                                                   target:self
                                                                   action:@selector(toolbarDoneButton:)];
    doneBarItem.tag=2;
    
    [self.keyToolBar setItems:[NSArray arrayWithObjects:spaceBarItem, doneBarItem, nil]];
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated
{
    
    keyBoardController.boardDelegate = nil;
    [keyBoardController release];
    
    [super viewDidDisappear:animated];
}


// 键盘的代理时间，是否允许弹出键盘，返回NO则不允许弹出键盘

- (BOOL)textFieldShoudStartEditing:(UITextField *)textField
{
    BOOL isNeedShowKeyBoard = NO;
    
    if (textField.tag == 3) {

        if (self.locateAndAreaPicker.view.superview) {
        
                [self.locateAndAreaPicker.view  removeFromSuperview];
            
            }

            [keyBoardController resignKeyboard:self.view];
        
        if (!self.baziPickerView.superview) {
            
            
            
            [self.view addSubview:self.baziPickerView];
            [self.baziPickerView setFrame:CGRectMake(0, self.view.frame.size.height, self.baziPickerView.frame.size.width, self.baziPickerView.frame.size.height)];
            [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height, 320, self.keyToolBar.frame.size.height)];

                [self.view addSubview:self.keyToolBar];

            [UIView animateWithDuration:0.3f animations:^(void){
                [self.baziPickerView setFrame:CGRectMake(0, self.view.frame.size.height-self.baziPickerView.frame.size.height, self.baziPickerView.frame.size.width, self.baziPickerView.frame.size.height)];
                [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height-self.baziPickerView.frame.size.height-self.keyToolBar.frame.size.height, 320, self.keyToolBar.frame.size.height)];


            }];
             
            [self.m_scrollView setFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-self.baziPickerView.frame.size.height-self.keyToolBar.frame.size.height)];
            [self.m_scrollView setContentSize:CGSizeMake(320, self.view.frame.size.height)];
            [self.m_scrollView scrollRectToVisible:textField.frame animated:YES];
            [self refreshBirthDayData];
            
            
        }
        else
        {
            [self.view bringSubviewToFront:self.baziPickerView];
        }
        
    }
    else if(textField.tag == 4)
    {
           [keyBoardController resignKeyboard:self.view];
        if (self.baziPickerView.superview) {
        
            [self.baziPickerView  removeFromSuperview];
            
        }
     
//        
//        if (self.locateAndAreaPicker) {
//
//            self.locateAndAreaPicker.delegate = nil;
//            [self.locateAndAreaPicker.view removeFromSuperview];
//            [self.locateAndAreaPicker release];
//             self.locateAndAreaPicker=nil;
//            [self.keyToolBar removeFromSuperview];
//        
//        }

        if (!self.locateAndAreaPicker.view.superview) {
            
        //[MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.locateAndAreaPicker = [[AreaPickerViewController alloc] initWithNibName:@"AreaPickerViewController" bundle:nil];
        [self.locateAndAreaPicker initWithStyle:self];
        self.locateAndAreaPicker.theResultTextField = textField;
        [self.view addSubview:self.locateAndAreaPicker.view];
        [self.locateAndAreaPicker.view setFrame:CGRectMake(0, self.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.locateAndAreaPicker.view.frame.size.height)];

            [self.view addSubview:self.keyToolBar];
            [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height,320,self.keyToolBar.frame.size.height)];
        [UIView animateWithDuration:0.3f animations:^(void){
            [self.locateAndAreaPicker.view setFrame:CGRectMake(0, self.view.frame.size.height-self.locateAndAreaPicker.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.locateAndAreaPicker.view.frame.size.height)];
            [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height-self.locateAndAreaPicker.view.frame.size.height-self.keyToolBar.frame.size.height, 320, self.keyToolBar.frame.size.height)];
            
            
        }];
        [self.m_scrollView setFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-self.locateAndAreaPicker.view.frame.size.height-self.keyToolBar.frame.size.height)];
        [self.m_scrollView setContentSize:CGSizeMake(320, self.view.frame.size.height)];
            
        self.locateAndAreaPicker.theResultTextField = textField;
        //[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.m_scrollView scrollRectToVisible:textField.frame animated:YES];
            
        }
        else
        {
            self.locateAndAreaPicker.theResultTextField = textField;
        }

       
    }
    else if (textField.tag == 5)
    {
        [keyBoardController resignKeyboard:self.view];
        if (self.baziPickerView.superview) {
            
            [self.baziPickerView  removeFromSuperview];
            
        }
//        if (self.locateAndAreaPicker) {
//
//             self.locateAndAreaPicker.delegate = nil;
//            [self.locateAndAreaPicker.view removeFromSuperview];
//            [self.locateAndAreaPicker release];
//            self.locateAndAreaPicker=nil;
//        }
        if (!self.locateAndAreaPicker.view.superview) {

           // [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            self.locateAndAreaPicker = [[AreaPickerViewController alloc] initWithNibName:@"AreaPickerViewController" bundle:nil];
            self.locateAndAreaPicker.theResultTextField = textField;
            [self.locateAndAreaPicker initWithStyle:self];
            [self.view addSubview:self.locateAndAreaPicker.view];
            [self.locateAndAreaPicker.view setFrame:CGRectMake(0, self.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.locateAndAreaPicker.view.frame.size.height)];

            [self.view addSubview:self.keyToolBar];
            [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height,320,self.keyToolBar.frame.size.height)];
            [UIView animateWithDuration:0.3f animations:^(void){
                [self.locateAndAreaPicker.view setFrame:CGRectMake(0, self.view.frame.size.height-self.locateAndAreaPicker.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.locateAndAreaPicker.view.frame.size.height)];
                [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height-self.locateAndAreaPicker.view.frame.size.height-self.keyToolBar.frame.size.height, 320, self.keyToolBar.frame.size.height)];


            }];
            
            [self.m_scrollView setFrame:CGRectMake(0, 0, 320, self.view.frame.size.height-self.locateAndAreaPicker.view.frame.size.height-self.keyToolBar.frame.size.height)];
            [self.m_scrollView setContentSize:CGSizeMake(320, self.view.frame.size.height)];
            self.locateAndAreaPicker.theResultTextField = textField;
           // [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.m_scrollView scrollRectToVisible:textField.frame animated:YES];
        }
        else
        {
            self.locateAndAreaPicker.theResultTextField = textField;
        }
        
    }


    else{

        [self.m_scrollView setFrame:CGRectMake(0, 0, 320, self.view.frame.size.height)];
        [self.m_scrollView setContentSize:CGSizeMake(320, self.view.frame.size.height)];
        
        if (self.locateAndAreaPicker.view.superview) {

            [UIView animateWithDuration:0.3f animations:^(void){
                [self.locateAndAreaPicker.view setFrame:CGRectMake(0, self.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.locateAndAreaPicker.view.frame.size.height)];
                [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height, 320, self.keyToolBar.frame.size.height)];
            }];
            
            [self.locateAndAreaPicker.view  removeFromSuperview];
            [self.keyToolBar removeFromSuperview];
            [self.locateAndAreaPicker release];
            self.locateAndAreaPicker = nil;
            
        }
        
        if (self.baziPickerView.superview) {

            [UIView animateWithDuration:0.3f animations:^(void){
                [self.baziPickerView setFrame:CGRectMake(0, self.view.frame.size.height, self.locateAndAreaPicker.view.frame.size.width, self.baziPickerView.frame.size.height)];
                [self.keyToolBar setFrame:CGRectMake(0, self.view.frame.size.height, 320, self.keyToolBar.frame.size.height)];
            }];
            [self.baziPickerView  removeFromSuperview];
            [self.keyToolBar removeFromSuperview];
        }

        isNeedShowKeyBoard = YES;
        

    }
    
  
    return isNeedShowKeyBoard;
    
}


#pragma pickeDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    if (!(pickerView == self.baziPickerView)) {

        return 0;
    }
    return 5;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if (!(pickerView == self.baziPickerView)) {

        return 0;
    }
    switch (component) {
        case 0:
            return [self.yearArr count];
            break;
        case 1:
            return [self.monthArr count];
            break;
        case 2:
            return [self.dayArr count];
            break;
        case 3:
            return [self.hourArr count];
            break;
        case 4:
            return [self.minArr count];
            break;
    }
    return 0;
}
-(void)refreshBirthDayData
{
    UILabel *label = (UILabel *)[self.baziPickerView viewForRow:[self.baziPickerView selectedRowInComponent:0] forComponent:0];
    NSString *string = label.text;

    UILabel *label1 = (UILabel *)[self.baziPickerView viewForRow:[self.baziPickerView selectedRowInComponent:1] forComponent:1];
    NSString *string1 = label1.text;

    UILabel *label2 = (UILabel *)[self.baziPickerView viewForRow:[self.baziPickerView selectedRowInComponent:2] forComponent:2];
    NSString *string2 = label2.text;

    UILabel *label3 = (UILabel *)[self.baziPickerView viewForRow:[self.baziPickerView selectedRowInComponent:3] forComponent:3];
    NSString *string3 = label3.text;

    UILabel *label4 = (UILabel *)[self.baziPickerView viewForRow:[self.baziPickerView selectedRowInComponent:4] forComponent:4];
    NSString *string4 = label4.text;

    UITextField *textField = (UITextField *)[self.view viewWithTag:3];
    textField.text = [NSString stringWithFormat:@"%@%@%@%@%@",string,string1,string2,string3,string4];
}

// 设置当前行的内容，若果行没有显示则自动释放
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (!(pickerView == self.baziPickerView)) {

        return;
    }
    
    if (component == 0) {

        int yearNum = [[self.yearArr objectAtIndex:[pickerView selectedRowInComponent:0]] intValue] ;
        int endDate = 0;
        switch ([pickerView selectedRowInComponent:1]+1) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                endDate = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                endDate = 30;
                break;
            case 2:

        if (yearNum % 4 == 0) {
            
            endDate = 29;
        } else {
            
            endDate = 28;
        
        }
    
    [self.dayArr removeAllObjects];
    for (NSInteger i =1; i<=endDate; i++) {

        [self.dayArr addObject:[NSString stringWithFormat:@"%d",i]];

    }
    [pickerView reloadComponent:2];

        }
    }

    if (component == 1) {
        int endDate = 0;
        int yearNum = [[self.yearArr objectAtIndex:[pickerView selectedRowInComponent:0]] intValue] ;
        switch (row+1) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                endDate = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                endDate = 30;
                break;
            case 2:
                // 是否为闰年
                if (yearNum % 4  == 0) {
                    endDate = 29;
                } else {
                    endDate = 28;
                    }
                
                break;
            default:
                break;
        }

        [self.dayArr removeAllObjects];
        for (NSInteger i =1; i<=endDate; i++) {

            [self.dayArr addObject:[NSString stringWithFormat:@"%d",i]];
            
        }
        [pickerView reloadComponent:2];

    }
    [self refreshBirthDayData];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (!(pickerView == self.baziPickerView)) {

        return nil;
    }
    UILabel *myView = nil;
    myView = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)] autorelease];
    myView.textAlignment = UITextAlignmentCenter;
    myView.font = [UIFont boldSystemFontOfSize:17.0f];      //用label来设置字体大小
    myView.backgroundColor = [UIColor clearColor];
    switch (component) {
        case 0:
            myView.text =  [NSString stringWithFormat:@"%@年",[self.yearArr objectAtIndex:row]];
            break;
        case 1:
            myView.text =  [NSString stringWithFormat:@"%@月",[self.monthArr objectAtIndex:row]];
            break;
        case 2:
            myView.text =  [NSString stringWithFormat:@"%@日",[self.dayArr objectAtIndex:row]];
            break;
        case 3:
            myView.text =  [NSString stringWithFormat:@"%@时",[self.hourArr objectAtIndex:row]];
            break;
        case 4:
            myView.text =  [NSString stringWithFormat:@"%@分",[self.minArr objectAtIndex:row]];
            break;
        default:
            break;
    }
    
    return myView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

{

    switch (component) {
        case 0:
            return 64.0;
            break;
        case 1:
            return 50;
            break;
        case 2:
            return 50;
            break;
        case 3:
            return 50;
            break;
        case 4:
            return 50;
            break;
        default:
            break;
    }
    return 0.0;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{

    return 36.0f;
}

#pragma ----
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.yearArr = [NSMutableArray array];
    self.monthArr = [NSMutableArray array];
    self.dayArr = [NSMutableArray array];
    self.hourArr = [NSMutableArray array];
    self.minArr = [NSMutableArray array];
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |
    NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
    comps = [calendar components:unitFlags fromDate:currentDate];
    int currentYear=[comps year];

    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"userInfo"];
    if (dic) {

        UITextField *textfield1 = (UITextField *)[self.view viewWithTag:1];
        UITextField *textfield2 = (UITextField *)[self.view viewWithTag:2];
        UITextField *textfield3 = (UITextField *)[self.view viewWithTag:3];
        UITextField *textfield4 = (UITextField *)[self.view viewWithTag:4];
        UITextField *textfield5 = (UITextField *)[self.view viewWithTag:5];

        textfield1.text = [dic objectForKey:@"name"];
        textfield2.text = [dic objectForKey:@"cellPhoneNum"];
        textfield3.text = [dic objectForKey:@"birthday"];
        textfield4.text = [dic objectForKey:@"nowlivingPlace"];
        textfield5.text = [dic objectForKey:@"birthPlace"];

        UISegmentedControl *control = (UISegmentedControl *)[self.view viewWithTag:10];
        
        NSString *string = [dic objectForKey:@"sexuality"];
        if ([string isEqualToString:@"男"]) {

            control.selectedSegmentIndex = 0;
            
        }
        else
        {
            control.selectedSegmentIndex = 1;
        }
    }

    for(int i=1930; i<=currentYear ; i++){
        [self.yearArr addObject:[NSString stringWithFormat:@"%d", i ]];
    }
    for(int i=1;i<=12;i++){
        [self.monthArr addObject:[NSString stringWithFormat:@"%d", i] ];
    }
    for( int i=1; i<= 31; i++){
        [self.dayArr addObject:[NSString stringWithFormat:@"%d",i]];
    }
    for(int i=1; i<=24; i++){
        [self.hourArr addObject:[NSString stringWithFormat:@"%d",i]];
    }
    for (int i = 1; i<=60; i++) {
        
        [self.minArr addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    [self.baziPickerView selectRow:50 inComponent:0 animated:YES];
    [self.baziPickerView selectRow:0 inComponent:1 animated:YES];
    [self.baziPickerView selectRow:0 inComponent:2 animated:YES];
    [self.baziPickerView selectRow:0 inComponent:3 animated:YES];
    [self.baziPickerView selectRow:0 inComponent:4 animated:YES];

}

- (void)pickerDidChaneStatus:(AreaPickerViewController *)picker withlocationInformation:(NSDictionary *)locationDic
{
     UITextField *textField4 = (UITextField *)[self.view viewWithTag:4];
     UITextField *textField5 = (UITextField *)[self.view viewWithTag:5];
    if (picker.theResultTextField == textField4) {

        

        NSString *string = [locationDic objectForKey:@"sheng"];


        NSString *string1 = [locationDic objectForKey:@"shi"];


        NSString *string2 = [locationDic objectForKey:@"qu"];

        UITextField *textField = (UITextField *)[self.view viewWithTag:4];
        textField.text = [NSString stringWithFormat:@"%@%@%@",string,string1,string2];
        
    }
    else if (picker.theResultTextField == textField5)
    {
        NSString *string = [locationDic objectForKey:@"sheng"];

        NSString *string1 = [locationDic objectForKey:@"shi"];
        
        NSString *string2 = [locationDic objectForKey:@"qu"];
        
        UITextField *textField = (UITextField *)[self.view viewWithTag:5];
        textField.text = [NSString stringWithFormat:@"%@%@%@",string,string1,string2];
    }

    
}
-(IBAction)doneBtnAction:(id)sender
{
    UITextField *textfield1 = (UITextField *)[self.view viewWithTag:1];
    UITextField *textfield2 = (UITextField *)[self.view viewWithTag:2];
    UITextField *textfield3 = (UITextField *)[self.view viewWithTag:3];
    UITextField *textfield4 = (UITextField *)[self.view viewWithTag:4];
    UITextField *textfield5 = (UITextField *)[self.view viewWithTag:5];

    if ([textfield1.text isEqualToString:@""]||[textfield2.text isEqualToString:@""]||[textfield3.text isEqualToString:@""]||[textfield4.text isEqualToString:@""]||[textfield5.text isEqualToString:@""]) {

        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"提示"
                                                            message:@"信息尚未填写完善"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
        [alertView show];
        [alertView release];

        return;
    }
    [self toolbarDoneButton:nil];
    UISegmentedControl *control = (UISegmentedControl *)[self.view viewWithTag:10];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];

    NSString *string1 = [NSString stringWithFormat:@"%@",textfield1.text];
    [dic setObject:string1 forKey:@"name"];

    NSString *string2 = [NSString stringWithFormat:@"%@",textfield2.text];
    [dic setObject:string2 forKey:@"cellPhoneNum"];
    

    

    NSString *sex = [control titleForSegmentAtIndex:control.selectedSegmentIndex] ;
    [dic setObject:sex forKey:@"sexuality"];
    
    NSString *string3 = [NSString stringWithFormat:@"%@",textfield3.text];
    [dic setObject:string3 forKey:@"birthday"];

    NSString *string4 = [NSString stringWithFormat:@"%@",textfield4.text];
    [dic setObject:string4 forKey:@"nowlivingPlace"];

    NSString *string5 = [NSString stringWithFormat:@"%@",textfield5.text];
    [dic setObject:string5 forKey:@"birthPlace"];

    [[NSUserDefaults standardUserDefaults] setObject:dic  forKey:@"userInfo"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"信息保存成功" message:nil
                                                   delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
